#include "ESP_HT.h"

HardwareSerial Serial_ht32(2); 

uint8_t h_1b = 0;
uint8_t h_2b = 0; 
bool success_hongwai = false;

struct data Temperture;
struct data Humidity;

void ESPHT_Init()
{
    Serial_ht32.begin(9600, SERIAL_8N1, 19, 17);
}

void  ESPHT_Send(uint8_t data)
{
    uint8_t cmd[] = {0x24,0x00,0x19};
    cmd[1] = data;
    Serial_ht32.write(cmd,3);
    Serial.write(cmd,3);
}

void ESPHT_Read()
{

    static uint8_t buf[4];
    static uint8_t state = 0;
    
    while (Serial_ht32.available()) 
    {
        uint8_t byte = Serial_ht32.read();

    switch (state) {
      case 0:
        if (byte == 0x24) {
          buf[0] = byte;
          state = 1;
        }
        break;

      case 1:
        buf[1] = byte;
        state = 2;
        break;

      case 2:
        buf[2] = byte;
        state = 3;
        break;

      case 3:
        buf[3] = byte;
        if (buf[3] == 0x19)
        {
            // Serial.println("Frame Received!");
            

            switch(buf[1])
            {
                case 0x30:
                {
                    h_1b = buf[2];
                    // Serial.println(buf[2], HEX);    
                    break;
                }
                case 0x31:
                {
                    h_2b = buf[2];
                    // Serial.println(buf[2], HEX); 
                    break;   
                }
                case 0x32:
                {
                    if(buf[2] == 0x72)
                    {
                        success_hongwai = true;
                        // Serial.println("success");                                 
                    }
                    else if(buf[2] == 0x71)
                    {
                        success_hongwai = false;
                    }
                    break;  
                }
                case 0x33:
                {
                    Temperture.shiwei = buf[2];
                    // Serial.println(buf[2], HEX);  
                    break;
                }
                case 0x34:
                {
                    Temperture.gewei = buf[2];
                    // Serial.println(buf[2], HEX);  
                    break;
                }
                case 0x35:
                {
                    Temperture.xiaoshu = buf[2];
                    // Serial.println(buf[2], HEX);  
                    devstate.temperature = String(Temperture.shiwei * 10 + Temperture.gewei + Temperture.xiaoshu / 10.0);
                    break;
                }
                case 0x36:
                {
                    Humidity.shiwei = buf[2];
                    // Serial.println(buf[2], HEX);  
                    break;
                }
                case 0x37:
                {
                    Humidity.gewei = buf[2];
                    // Serial.println(buf[2], HEX);  
                    break;
                }
                case 0x38:
                {
                    Humidity.xiaoshu = buf[2];
                    // Serial.println(buf[2], HEX);  
                    devstate.humidity = String(Humidity.shiwei * 10 + Humidity.gewei + Humidity.xiaoshu / 10.0);
                    break;
                }

            }            
        }
        state = 0; // 重置状态机，准备下一帧
       for(int i = 0; i < 4;i ++)
       {
           buf[i] = 0;
       }
        break;
    }
  }
}